/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
 
/***************************************************************************
 * Name: vos_os_posix.c
 *
 * Purpose: virtual os API implement in posix system
 *
 * Developer:
 *   wen.gu , 2014-12-25
 *
 * TODO: 
 *
 ***************************************************************************/

//inner header file
#include "sys_config.h"

#if defined(HAVE_POSIX_OS_API) && HAVE_POSIX_OS_API
//API header file

#include "vos_sys.h"

#include <dlfcn.h>
#include <fcntl.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
/***************************************************************************
 *
 * VOS Sleep API define
 *
 ***************************************************************************/
/**
*@brief  do sleep unit in millisecond
*
*@param  ms [in] sleep time count
*
*@retuen None.
*@see 
*/
void vosSleepMs(GU32 ms)
{
    GU32 sec = ms / 1000;
    GU32 us = (ms % 1000) * 1000;
    if (sec)
    {
        sleep(sec); //sleep unit in second
    }
    if (us)
    {
        usleep(us);
    }
}

/**
*@brief  do sleep unit in second
*
*@param  sec [in] sleep time count
*
*@retuen None.
*@see 
*/
void vosSleepSec(GU32 sec)
{
     sleep(sec); //sleep unit in second
}

/***************************************************************************
 *
 * VOS System Time API define
 *
 ***************************************************************************/

/**
*@brief  get current time unit in microsecond(us)
*
*@param  None.
*
*@retuen current time unit in microsecond(us)
*@see 
*/
GS64 vosSysTimeUs(void)
{
    struct timeval tv;
    gettimeofday(&tv,NULL);
    return (GS64)tv.tv_sec * 1000000 + tv.tv_usec;
}

/**
*@brief  get current time unit in millisecond
*
*@param  None.
*
*@retuen current time unit in millisecond
*@see 
*/
GS64 vosSysTimeMs(void)
{
    struct timeval tv;
    gettimeofday(&tv,NULL);
    return (GS64)tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

/**
*@brief  get current date and time
*
*@param  dt [out]  return current data and time
*
*@retuen None.
*@see 
*/
void vosSysDateTime(vosDate_t *dt)
{    
    time_t time1; 
    struct tm *when; 

    time(&time1); 
    when = localtime(&time1); 
    dt->year = when->tm_year + 1900; 
    dt->mon = when->tm_mon + 1; 
    dt->day = when->tm_mday; 
    dt->hour = when->tm_hour; 
    dt->min = when->tm_min; 
    dt->sec = when->tm_sec; 
}

/***************************************************************************
 *
 * VOS Dynamic library(or share library) API define
 *
 ***************************************************************************/
/**
*@brief  load a Dynamic library(or share library)
*
*@param  dllname [in]  dynamic library path and name
*
*@retuen if success return dl handle,else return 'NULL'.
*@see 
*/
GPHD vosLoadDLL(const GS08 *dllname)
{
    return (GPHD)dlopen((const char *)dllname, RTLD_NOW);
}

/**
*@brief  get a symbole from Dynamic library(or share library)
*
*@param  dllHd  [in]  dynamic library handle
*@param  symbol [in]  symbol name
*
*@retuen if success return symbol pointer,else return 'NULL'.
*@see 
*/
void* vosGetDLLSymbol(GPHD dllHd, const GS08 *symbol)
{
    if (dllHd && symbol)
    {
        return dlsym(dllHd,symbol);
    }

    return NULL;
}

/**
*@brief  free a Dynamic library(or share library) handle.
*
*@param  dllHd  [in]  dynamic library handle
*
*@retuen None.
*@see 
*/
void vosFreeDLL(GPHD dllHd)
{
    if (dllHd)
    {
        dlclose(dllHd);
    }
}

#endif